package com.gloaba.common.mq.utils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import com.gloaba.common.core.utils.JsonUtils;

@Component
@Slf4j
@RequiredArgsConstructor
public class MqUtils {

    private final RabbitTemplate rabbitTemplate;

    /**
     * 发送数据到消息队列，QUEUE方式,默认优先级1
     */
    public void sendQueue(String queue, Object obj) {
        sendQueue( queue, obj, 1);
    }

    /**
     * 发送数据到消息队列，QUEUE方式
     */
    public void sendQueue(String queue, Object obj, Integer pri)  {
        log.debug("sendRabbitMQ queue:{} messageObj:{} start", queue, JsonUtils.toJsonString(obj));
        rabbitTemplate.convertAndSend(queue, JsonUtils.toJsonString(obj), message -> {
            message.getMessageProperties().setPriority(pri);
            return message;
        });
        log.debug("sendRabbitMQ queue:{} messageObj:{} ok", queue,JsonUtils.toJsonString(obj));
    }

    /**
     * 发送数据到消息队列，exchange方式,默认优先级1
     */
    public void sendExchane(String exchange, String routingkey, Object obj) {
        sendExchane( exchange, routingkey, obj, 1);
    }

    /**
     * 发送数据到消息队列，exchange方式
     */
    public void sendExchane(String exchange, String routingkey, Object obj, Integer pri) {
        log.debug("sendRabbitMQ exchange:{} routingkey:{} messageObj:{} start", exchange,routingkey,JsonUtils.toJsonString(obj));
        rabbitTemplate.convertAndSend(exchange, routingkey, obj, message -> {
            message.getMessageProperties().setPriority(pri);
            return message;
        });
        log.debug("sendRabbitMQ exchange:{} routingkey:{} messageObj:{} ok", exchange,routingkey,JsonUtils.toJsonString(obj));
    }


}
